package com.example.test.conview.view.core;

import static com.puty.conputyview.common.PutyConst.HOUSEHOLD_SERIES;
import static com.puty.conputyview.common.PutyConst.INDUSTRY_SERIES;
import static com.puty.conputyview.common.PutyConst.SERIES_TYPE;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;

import com.puty.conputyutils.ArrayUtls;
import com.puty.conputyutils.MultiLanguageUtils;
import com.puty.conputyutils.StringUtils;
import com.puty.conputyview.R;
import com.puty.conputyview.common.PutyConst;
import com.puty.conputyview.view.tool.Global;

public class Element extends BaseElement{

    /**
     * 画笔
     */
    private Paint mPaint;

    /**
     * 斜角
     */
    public static Bitmap puBitmap;

    /**
     * 上下
     */
    public static Bitmap puvBitmap;

    /**
     * 水平
     */
    public static Bitmap puhBitmap;

    /**
     * 删除Bitmap
     */
    public static Bitmap deleteBitmap;

    /**
     * 缩放图标宽
     */
    public int scaleImageWidth = 25;


    /**
     * //渲染时的缩放比例，解决标签镜像时，元素和镜像部分的线条虚线框显示不一致的bug
     */
    public float renderScale = 1.0f;

    public Element(Context context, Label lb) {
        super(context, lb);
        if (SERIES_TYPE == HOUSEHOLD_SERIES) {
            initHousehold();
        } else if(SERIES_TYPE == INDUSTRY_SERIES) {
            initIndustry();
        }
    }

    /**
     * 家用系列初始化数据
     */
    private void initHousehold() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(super.mContext.getResources().getColor(R.color.c_3d68e8));
        mPaint.setStrokeWidth(3);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setPathEffect(new DashPathEffect(new float[]{5, 5}, 0));
    }

    /**
     * 工业系列初始化数据
     */
    private void initIndustry() {
        if (puBitmap == null) {
            puBitmap = BitmapFactory.decodeResource(super.mContext.getResources(), R.mipmap.
                    diagonal_expansion_button);
        }
        if (puvBitmap == null) {
            puvBitmap = BitmapFactory.decodeResource(super.mContext.getResources(), R
                    .mipmap.up_and_down_expansion_button);
        }
        if (puhBitmap == null) {
            puhBitmap = BitmapFactory.decodeResource(super.mContext.getResources(), R
                    .mipmap.left_and_right_expansion_button);
        }
        if (deleteBitmap == null) {
            deleteBitmap = BitmapFactory.decodeResource(super.mContext.getResources(), R
                    .mipmap.ic_delete_label_element);
        }
        scaleImageWidth = deleteBitmap.getWidth();
    }

    /**
     * 复制对象
     *
     * @param lb 标签实体类
     * @return Element对象
     * @throws CloneNotSupportedException
     */
    public Element clone(Label lb) throws CloneNotSupportedException {
        this.lb = lb;
        return clone();
    }

    /**
     * 复制对象
     *
     * @return Element对象
     * @throws CloneNotSupportedException
     */
    @Override
    public Element clone() throws CloneNotSupportedException {
        super.clone();
        Element el = this;
        switch (el.type) {
            case PutyConst.TEXT: // 文本
                TextElement txtelement = new TextElement(el.mContext, el.content, el.width, el
                        .height, lb);
                return txtelement.clone(el,txtelement);
            case PutyConst.BAR_CODE: // 一维码
                BarCodeOneElement barCodeOneElement = new BarCodeOneElement(el.mContext, el.content,
                        el.width / lb.scalingRatio, el.height / lb.scalingRatio, 0, lb);
                return barCodeOneElement.clone(el, barCodeOneElement);
            case PutyConst.QR_CODE: // 二维码
                QrCodeElement qrCodeElement = new QrCodeElement(el.mContext, this.content,
                        el.left, el.top, el.width / lb.scalingRatio,
                        el.height / lb.scalingRatio, lb);
                return qrCodeElement.clone(el, qrCodeElement);
            case PutyConst.IMAGE: // 图片
                ImageElement imageElement = new ImageElement(el.mContext, el.tempBitmap,
                        el.width, el.height, el.imageUrlString, lb);
                return imageElement.clone(el, imageElement);
            case PutyConst.DATE: // 日期
                TimeElement timeelement = new TimeElement(el.mContext, el.content, el.width, el.height, lb);
                return timeelement.clone(el, timeelement);
        }
        return el;
    }
    /**
     * 左对齐
     */
    protected void textalignLeft(float left, float top, float textHeight, String content, Paint
            textPaint, Canvas canvas) {
        textalignLeft(left, top, textHeight, content, textPaint, canvas, false,false);
    }

    /**
     * 左对齐
     *
     * @param left 左边
     * @param top 上边
     * @param textHeight 高度
     * @param content 内容
     * @param textPaint 画笔
     * @param canvas 画布
     * @param isAntiWhite 是否反白
     * @param foreground 是否白色
     */
    protected void textalignLeft(float left, float top, float textHeight, String content, Paint
            textPaint, Canvas canvas, boolean isAntiWhite, boolean foreground) {
        char[] arr = content.toCharArray();

        int len = arr.length;
        float width = this.width / compressionRatio;
        float height = this.height / compressionRatio;
        if (PutyConst.RATE_90 == rate || PutyConst.RATE_270 == rate) {
            width = this.height / compressionRatio;
            height = this.width / compressionRatio;
        }
        // 反白
        if (isAntiWhite) {
            float rectTop = 0f;
            float rectBottom = 0f;
            if (SERIES_TYPE == HOUSEHOLD_SERIES) {
                rectBottom = height;
            } else if (SERIES_TYPE == INDUSTRY_SERIES) {
                rectTop = top;
                rectBottom = top + height;
            }
            textPaint.setColor(Color.WHITE);
            Paint p = new Paint();
            p.setColor(Color.BLACK);// 设置颜色
            p.setStyle(Paint.Style.FILL);//设置填满
            canvas.drawRect(left, rectTop, left + width, rectBottom, p);// 矩形
        } else  if (foreground){
            textPaint.setColor(Color.WHITE);
        }else{
            textPaint.setColor(Color.BLACK);
        }
        if ((MultiLanguageUtils.isRTL(mContext) || StringUtils.textContainsKhmer(content)) && type == 1) {
            float drawY = SERIES_TYPE == HOUSEHOLD_SERIES ? (height + textHeight) / 2 :
                    top + textHeight - textHeight / 5;
            canvas.drawText(content, left, drawY, textPaint);
        } else {
            float baseline = 0;
            if (SERIES_TYPE == HOUSEHOLD_SERIES) {
                Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
                float distance = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
                baseline=height/2+distance;
            } else if (SERIES_TYPE == INDUSTRY_SERIES) {
                baseline =top + textHeight - textHeight / 5;
            }
            for (int i = 0; i < len; i++) {
                String vString = String.valueOf(arr[i]);
                //单个字体宽度
                float textWidth = getContentWhidth(textPaint, vString);
                canvas.drawText(vString, left, baseline, textPaint);
                left += textWidth + textCellSpace * 8 * lb.scale;
            }
        }
    }

    /**
     * 是否缩放
     */
    public BaseElement isscale(float xShaft, float yShaft) {
        //二维码 判断是否点击缩放区域 右下角
        if ((xShaft >= this.left + this.width - this.scaleImageWidth / 2 &&
                xShaft <= this.left + this.width + this.scaleImageWidth / 2)
                && (yShaft >= this.top + this.height - this.scaleImageWidth / 2 &&
                yShaft <= this.top + this.height + this.scaleImageWidth / 2)) {
            return this;
        }
        return null;
    }

    /**
     * 是否删除
     */
    public BaseElement isDelete(float xShaft, float yShaft) {
        //二维码 判断是否点击缩放区域 右下角
        if ((xShaft >= this.left - this.scaleImageWidth / 2 &&
                xShaft <= this.left + this.scaleImageWidth / 2)
                && (yShaft >= this.top - this.scaleImageWidth / 2 &&
                yShaft <= this.top + this.scaleImageWidth / 2)) {
            return this;
        }
        return null;
    }

    /**
     * 获取字号下标
     * @param contentLength 开始下标
     * @param width 宽
     * @param height 高
     * @return 字号下标
     */
    protected int getFontIndex(int contentLength, float width, float height) {
        float min = width > height ? height : width;
        float suiValue = min * 0.3f;
        float[] fontSize = new float[Global.fontSize.length];
        for (int i = 0; i < fontSize.length; i++) {
            fontSize[i] = contentLength * Global.fontSize[i];
        }
        int index = ArrayUtls.find(fontSize, 0, fontSize.length - 1, suiValue);
        return index;
    }
}
